/* CHUONG TRINH QUAN LY CUA HANG SACH
* PHAN GIAO DIEN DANH CHO KHACH HANG
* Cac chuc nang cua chuong trinh
	Dang nhap bang tai khoan rieng
	Dang ky tai khoan moi
	Duyet sach trong thu vien
	Tim kiem sach
	Cho sach yeu thich vao gio
	Mua sach co trong gio
*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include "MyFunc.c"
#include "DeclearFunc.c"

void main() {
	int choose, tempAddCart;
	char nameOfBook[255], chooseYN, tempMail[255], chooseCart;
	//=> Dang nhap
	loginCustomer(tempMail);
	//=> Menu chinh
	while(1) {
		cls();
		printf("\n\t\t=============*AMAZON BOOKSTORE*=============");
		printf("\n\n\t\t\t      MENU CHUC NANG");
		printf("\n\n\t\t+===================================================+");
		printf("\n\n\t\t1. Duyet sach");
		printf("\n\n\t\t2. Tim kiem sach");
		printf("\n\n\t\t3. Mua sach co trong gio hang");
		printf("\n\n\t\t4. Thoat ung dung");
		printf("\n\n\t\t+===================================================+");
		printf("\n\n\t\tMoi ban chon chuc nang: ");
		scanf("%d", &choose);
		switch(choose) {
			//=> Duyet sach
			case 1:
				cls();
				printf("\n\t\t=============*AMAZON BOOKSTORE*=============");
				printf("\n\n\t\t\t\tTHU VIEN SACH");
				showLibBook(tempMail);
				break;
			//=> Tim kiem sach
			case 2:
				while(1) {
					cls();
					printf("\n\t\t=============*AMAZON BOOKSTORE*=============");
					printf("\n\n\t\t\tTIM KIEM SACH TRONG THU VIEN\n\n");
					printf("\t\t     ------------------------------------\n\n");
					printf("\t\tNhap ten sach can xem thong tin: \n\n\t\t");
					fflush(stdin);
					gets(nameOfBook);
					showOneBook(nameOfBook);
					printf("\n\tCho cuon sach nay vao gio?(C/K): ");
					scanf("%c", &chooseCart);
					if(chooseCart == 'c' || chooseCart == 'C') {
						tempAddCart	= addCart(nameOfBook, tempMail);
						if(tempAddCart) {
							printf("\n\tSach da duoc them vao gio!");
						} else {
							printf("\n\tSach da co trong gio");
						}
					}
					fflush(stdin);
					printf("\n\n\tBan muon tim kiem them sach?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'N' || chooseYN == 'n') {
						break;
					}
					fflush(stdin);
				}
				flushCache(1);
				break;
			//=> Mua sach trong gio hang
			case 3:
				cls();
				printf("\n\t\t=============*AMAZON BOOKSTORE*=============");
				printf("\n\n\t\t\tTIM KIEM SACH TRONG THU VIEN\n\n");
				printf("\t\t     ------------------------------------\n\n");
				printf("\t\tNhap ten sach muon mua: \n\n\t\t");
				fflush(stdin);
				gets(nameOfBook);
				buyBook(tempMail, nameOfBook);
				flushCache(1);
				break;
			//=> Thoat ung dung
			case 4:
				exit(0);
			default:
				printf("\n\n\t\tNhap sai. Moi ban nhap lai");
				flushCache(1);
				break;
		}
	}
}

//=> Dang nhap
void loginCustomer(char *email) {
	customerInfo customerInfo;
	char user[255], pass[255];
	int choose, isValidLogin, chooseYN, isValidAddUser;
	while(!isValidLogin) {
		cls();
		printf("\n\t\t========*CHAO MUNG BAN DEN VOI AMAZON BOOKSTORE*========\n\n");
		printf("\t\t+===================================================+\n\n");
		printf("\t\t1. Dang nhap\n\n");
		printf("\t\t2. Dang ky tai khoan\n\n");
		printf("\t\t3. Thoat ung dung\n\n");
		printf("\t\t+===================================================+\n\n");
		printf("\t\t*Luu y nhap so tu 1 -> 3 ung voi moi chuc nang\n\n");
		printf("\t\tMoi ban chon: ");
		scanf("%d", &choose);
		switch(choose) {
			//=> Dang nhap
			case 1:
				while(1) {
					cls();
					printf("\n\t\t========*CHAO MUNG BAN DEN VOI AMAZON BOOKSTORE*========\n\n");
					printf("\t\t\tNhap tai khoan va mat khau cua ban\n\n");
					printf("\n\n\t\tUser: ");
					scanf("%s", user);
					printf("\n\t\tPassword: ");
					scanf("%s", pass);
					isValidLogin = valiLogin(user, pass);
					if(!isValidLogin) {
						printf("\n\t\tTai khoan hoac mat khau khong dung\n\n");
						flushCache(1);
					} else {
						break;
					}
					break;
				}
				strcpy(email, user);
				fflush(stdin);
				break;

			//=> Dang ky tai khoan
			case 2:
				while(1) {
					cls();
					printf("\n\t\t========*CHAO MUNG BAN DEN VOI AMAZON BOOKSTORE*========\n\n");
					printf("\t\t\t\t   DANG KY TAI KHOAN\n\n");
					printf("\t\t---------------------------------------\n\n");
					fflush(stdin);
					printf("\t\tHo ten: ");
					gets(customerInfo.name);
					fflush(stdin);
					printf("\n\t\tDia chi: ");
					gets(customerInfo.address);
					fflush(stdin);
					printf("\n\t\tSo dien thoai: ");
					gets(customerInfo.phone);
					fflush(stdin);
					printf("\n\t\tTuoi: ");
					gets(customerInfo.age);
					fflush(stdin);
					printf("\n\t\tGioi tinh: ");
					gets(customerInfo.sex);
					fflush(stdin);
					printf("\n\t\tNghe nghiep: ");
					gets(customerInfo.job);
					fflush(stdin);
					printf("\n\t\tEmail: ");
					gets(customerInfo.email);
					fflush(stdin);
					printf("\n\t\tPassword: ");
					gets(customerInfo.password);
					fflush(stdin);
					printf("\n\t\tSo tai khoan ngan hang(14 so): ");
					gets(customerInfo.bankAccount);
					printf("\n\t\t---------------------------------------");
					isValidAddUser = addUser(customerInfo.name, customerInfo.address, customerInfo.phone, customerInfo.age, customerInfo.sex,
											customerInfo.bankAccount, customerInfo.job, customerInfo.email, customerInfo.password);
					fflush(stdin);
					if(!isValidAddUser) {
						printf("\n\t\tThong tin ban vua nhap co sai sot. Vui long xem lai\n\n");
					} else {
						printf("\n\t\tChuc mung. Ban da dang ky thanh cong");
					}
					flushCache(1);
					break;
				}
				fflush(stdin);
				break;
			case 3:
				exit(0);
			default:
				printf("\n\t\tNhap sai. Moi ban nhap lai");
				flushCache(1);
				break;
		}
	}
}

//=> Duyet sach trong thu vien
void showLibBook(char *email) {
	FILE *f;
	bookInfo bookInfo[100];
	char link[255], tempNameBook[255];
	int sumBook, i = 0, choose = 1, tempAddCart;
	f = fopen("Library\\Books.amazon", "r");
	fscanf(f, "%d", &sumBook);
	while(1) {
		cls();
		printf("\n\t\t========*CHAO MUNG BAN DEN VOI AMAZON BOOKSTORE*========\n\n");
		printf("\t\t\t\t   THU VIEN SACH");
		for( ; i < sumBook; i++) {
			fscanf(f, "\n%s", bookInfo[i].name);
		}
		fclose(f);
		for(i = 0; i < sumBook; i++) {
			strcpy(tempNameBook, bookInfo[i].name);
			pathFile(tempNameBook);
			strcpy(link, LINK_LIBRARY);
			strcat(link, tempNameBook);
			f = fopen(link, "r");
			fscanf(f, "%s\n", bookInfo[i].author);
			addSpace(bookInfo[i].author);
			fscanf(f, "%s\n", bookInfo[i].publisher);
			addSpace(bookInfo[i].publisher);
			fscanf(f, "%d\n", &bookInfo[i].releaseYear);
			fscanf(f, "%d", &bookInfo[i].price);
			fclose(f);
			addSpace(bookInfo[i].name);
			printf("\n\t\t-------------------------------------------");
			printf("\n\t\tSTT: %d", i + 1);
			printf("\n\t\tTen sach: %s", bookInfo[i].name);
			printf("\n\t\tTac gia: %s", bookInfo[i].author);
			printf("\n\t\tNXB: %s", bookInfo[i].publisher);
			printf("\n\t\tNam xuat ban: %d", bookInfo[i].releaseYear);
			printf("\n\t\tGia: %d VND", bookInfo[i].price);
			printf("\n\t\t-------------------------------------------");
		}
		printf("\n\n\t\tNhap so ung voi STT sach de cho vao gio. Nhap '0' de quay lai: ");
		scanf("%d", &choose);
		fflush(stdin);
		if(choose == 0) {
			break;
		}
		if(choose >= 1 && choose <= sumBook) {
			tempAddCart = addCart(bookInfo[choose - 1].name, email);
			if(tempAddCart) {
				printf("\n\t\tSach da duoc them vao gio!");
			} else {
				printf("\n\t\tSach da co trong gio");
				}
		} else {
			printf("\n\t\tKhong tim thay sach!");
		}
	}
}

//=> Mua sach trong gio hang
void buyBook(char *email, char* nameOfBook) {
	FILE *f;
	bookInfo bookInfo;
	char tempNameBook[255], tempEmail[255], link[255], chooseYN;
	strcpy(tempNameBook, nameOfBook);
	deleteSpace(tempNameBook);
	pathFile(tempNameBook);
	strcpy(tempEmail, email);
	strcpy(link, LINK_CART);
	strcat(link, tempEmail);
	strcat(link, tempNameBook);
	if((f = fopen(link, "r")) != NULL){
		fscanf(f, "%s\n", bookInfo.name);
		addSpace(bookInfo.name);
		fscanf(f, "%s\n", bookInfo.author);
		addSpace(bookInfo.author);
		fscanf(f, "%s\n", bookInfo.publisher);
		addSpace(bookInfo.publisher);
		fscanf(f, "%d\n", &bookInfo.releaseYear);
		fscanf(f, "%d", &bookInfo.price);
		printf("\n\t\tCuon sach da duoc thanh toan");
		statisticBook(bookInfo.name, bookInfo.price);
		printf("\n\n\t\tBan co muon in hoa don khong?(C/K): ");
		fflush(stdin);
		scanf("%c", &chooseYN);
		if(chooseYN == 'C' || chooseYN == 'c') {
			cls();
			showBillBook(bookInfo.name, bookInfo.author, bookInfo.publisher, bookInfo.releaseYear, bookInfo.price);
		}
	} else {
		printf("\n\t\tHay them sach vao gio truoc khi mua!");
	}
	fflush(stdin);
}
 //=> In Hoa don
 void showBillBook(char* nameOfBook, char* author, char* publisher, int releaseYear, int price) {
	time_t now;
	time(&now);
	char *year, *month, *day;
	year = (char*) malloc(sizeof(year));
	month = (char*) malloc(sizeof(month));
	day = (char*) malloc(sizeof(day));
	struct tm*info;
	info = localtime(&now);
	strftime(year, 5, "%Y", info);
	strftime(month, 4, "%m", info);
	strftime(day, 4, "%d", info);
	printf("\n\t\t-------------HOA DON MUA HANG-------------");
	printf("\n\n\t\tNgay %s thang %s nam %s", day, month, year);
	printf("\n\n\t\tTen sach: %s", nameOfBook);
	printf("\n\n\t\tTac gia: %s", author);
	printf("\n\n\t\tNXB: %s", publisher);
	printf("\n\n\t\tNam xuat ban: %d", releaseYear);
	printf("\n\n\t\tGia tien: %d", price);
	printf("\n\t\t------------------------------------------");
 }
